package com.chen.nongansampling;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.chen.nongansampling.adapter.ListviewEditTextAdapter;
import com.chen.nongansampling.adapter.ListviewTextAdapter;
import com.chen.nongansampling.adapter.UpdateOperatorPresenter;
import com.chen.nongansampling.app.BaseApplication;
import com.chen.nongansampling.bean.AddCaiyangTypeBean;
import com.chen.nongansampling.bean.Callback;
import com.chen.nongansampling.bean.TianjiaCaiYnagRequest;
import com.chen.nongansampling.bean.getOperatingByPkId;
import com.chen.nongansampling.bean.ui.ListviewBean;
import com.chen.nongansampling.presenterView.UpdateOperatorView;

import java.util.ArrayList;
import java.util.List;

import static com.chen.nongansampling.fragment.ui.sample.SampleFragment.typelistAdd;

public class AddTypeActivity  extends AppCompatActivity implements UpdateOperatorView {
    private ListView listViewTypeName;
    private ListView listViewxiala;
    ListviewEditTextAdapter listviewEditTextAdapter;
    UpdateOperatorPresenter updateOperatorPresenter = new UpdateOperatorPresenter(AddTypeActivity.this);
    public TextView kaitou;
    public String typeid;
    public AddCaiyangTypeBean addCaiyangTypeBean = new AddCaiyangTypeBean();
    public List<ListviewBean> list;
    private   getOperatingByPkId ger;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_addtype);
        initToolbar();
        kaitou = findViewById(R.id.sample_tool_bar_nametype);
        listViewxiala = (ListView) findViewById(R.id.setting_xialatype);
        listViewTypeName = (ListView) findViewById(R.id.setting_listviewmtypename);

        ListviewBean<String> list1 = new ListviewBean();
        list1.setData("");
        list1.setImageSrc(R.mipmap.listview_sample_name);
        list1.setName("样品名称");
        list1.setRedStar(true);

        ListviewBean<String> list2 = new ListviewBean();
        list2.setData("请选择");
        list2.setImageSrc(R.mipmap.jingyingtype);
        list2.setName("样品类别");
        list2.setRedStar(true);

        List<ListviewBean> list = new ArrayList<>();

        list.add(list1);

        List<ListviewBean> listE = new ArrayList<>();
        listE.add(list2);

        listviewEditTextAdapter=new ListviewEditTextAdapter(list, getApplicationContext());
        listViewTypeName.setAdapter(listviewEditTextAdapter);

        listViewxiala.setAdapter(new ListviewTextAdapter(listE,getApplicationContext(),false));

        listViewxiala.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (position)
                {
                    case 0:{

                        if (typelistAdd.getData()!=null) {

                            String s[]=new String[typelistAdd.getData().size()];
                            for (int i=0;i<typelistAdd.getData().size();i++)
                            {
                                s[i]=typelistAdd.getData().get(i).getTypeName();
                            }

                            AlertDialog.Builder builder = new AlertDialog.Builder(AddTypeActivity.this);
                            builder.setTitle("请选择样品类别");
                            builder.setIcon(R.mipmap.xuanze);
                            builder.setItems(s, new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.dismiss();
                                    TextView t = view.findViewById(R.id.listview_text_data);
                                    t.setText(typelistAdd.getData().get(which).getTypeName());
                                    t.setTextColor(Color.BLACK);
                                    typeid=typelistAdd.getData().get(which).getTypeId();
                                }
                            });
                            builder.create().show();

                        }
                    }break;

                }
            }
        });
    }

    public void Onclick(View view){

        TianjiaCaiYnagRequest  TianjiaCaiYnagRequest =new TianjiaCaiYnagRequest();


        TianjiaCaiYnagRequest.setAccountPkId(BaseApplication.getAccount().getPkId());
        if(!TextUtils.isEmpty(typeid)){
            if(!TextUtils.isEmpty(listviewEditTextAdapter.list.get(0).getData().toString())&&!"请选择".equals(listviewEditTextAdapter.list.get(0).getData().toString())){


                addCaiyangTypeBean.setName(listviewEditTextAdapter.list.get(0).getData().toString());
                addCaiyangTypeBean.setTypeId(typeid);

                TianjiaCaiYnagRequest.settianjiacaiyang(addCaiyangTypeBean);
                updateOperatorPresenter.updateCaiYangResponseInfo(TianjiaCaiYnagRequest);
            }else{
                Toast.makeText(AddTypeActivity.this,"样品名称不能为空!!!!",Toast.LENGTH_LONG).show();
            }
        }else{
            Toast.makeText(AddTypeActivity.this,"样品类别不能为空!!!!",Toast.LENGTH_LONG).show();
        }


    }
    public void initToolbar(){
        Toolbar toolbar = (Toolbar)findViewById(R.id.toolbartype);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);//隐藏标题
        toolbar.setNavigationIcon(R.mipmap.back_white);       //加载图标
        toolbar.setTitle("");//经营户信息
        TextView title = findViewById(R.id.sample_tool_bar_nametype);
        title.setText("添加样品类别 ");
        toolbar.setTitleMarginStart(300);
        toolbar.setTitleTextColor(Color.WHITE);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {   //为图标设置监听器
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    public void UpdateOperator(Callback callback) {
       // System.out.println("我的callback.getState()是什么："+callback.getState());
        if (callback.getState()==1)
        {
            Toast.makeText(AddTypeActivity.this,"添加成功",Toast.LENGTH_LONG).show();
            Intent intent = new Intent(AddTypeActivity.this, MainActivity.class);
            startActivity(intent);

        }else if(callback.getState()==200){
            Toast.makeText(AddTypeActivity.this,callback.getAlertMessage(),Toast.LENGTH_LONG).show();
        }
    }

    @Override
    public void onError(String result) {
        Toast.makeText(AddTypeActivity.this,result,Toast.LENGTH_LONG).show();
    }

    @Override
    public void onResume() {
        super.onResume();
        updateOperatorPresenter.BindPresentView(this);
        updateOperatorPresenter.onCreate();
    }
}